/**
 ******************************************************************************
 * @file    Project/STM32F10x_StdPeriph_Template/stm32f10x_it.c
 * @author  MCD Application Team
 * @version V3.6.0
 * @date    20-September-2021
 * @brief   Main Interrupt Service Routines.
 *          This file provides template for all exceptions handler and
 *          peripherals interrupt service routine.
 ******************************************************************************
 * @attention
 *
 * Copyright (c) 2011 STMicroelectronics.
 * All rights reserved.
 *
 * This software is licensed under terms that can be found in the LICENSE file
 * in the root directory of this software component.
 * If no LICENSE file comes with this software, it is provided AS-IS.
 *
 ******************************************************************************
 */

/* Includes ------------------------------------------------------------------*/
#include "stm32f10x_it.h"

/** @addtogroup STM32F10x_StdPeriph_Template
 * @{
 */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/******************************************************************************/
/*            Cortex-M3 Processor Exceptions Handlers                         */
/******************************************************************************/

/**
 * @brief  This function handles NMI exception.
 * @param  None
 * @retval None
 */
void NMI_Handler(void)
{
}

/**
 * @brief  This function handles Hard Fault exception.
 * @param  None
 * @retval None
 */
void HardFault_Handler(void)
{
  /* Go to infinite loop when Hard Fault exception occurs */
  while (1)
  {
  }
}

/**
 * @brief  This function handles Memory Manage exception.
 * @param  None
 * @retval None
 */
void MemManage_Handler(void)
{
  /* Go to infinite loop when Memory Manage exception occurs */
  while (1)
  {
  }
}

/**
 * @brief  This function handles Bus Fault exception.
 * @param  None
 * @retval None
 */
void BusFault_Handler(void)
{
  /* Go to infinite loop when Bus Fault exception occurs */
  while (1)
  {
  }
}

/**
 * @brief  This function handles Usage Fault exception.
 * @param  None
 * @retval None
 */
void UsageFault_Handler(void)
{
  /* Go to infinite loop when Usage Fault exception occurs */
  while (1)
  {
  }
}

/**
 * @brief  This function handles SVCall exception.
 * @param  None
 * @retval None
 */
void SVC_Handler(void)
{
}

/**
 * @brief  This function handles Debug Monitor exception.
 * @param  None
 * @retval None
 */
void DebugMon_Handler(void)
{
}

/**
 * @brief  This function handles PendSVC exception.
 * @param  None
 * @retval None
 */
void PendSV_Handler(void)
{
}

/**
 * @brief  This function handles SysTick Handler.
 * @param  None
 * @retval None
 */
void SysTick_Handler(void)
{
}

/******************************************************************************/
/*                 STM32F10x Peripherals Interrupt Handlers                   */
/*  Add here the Interrupt Handler for the used peripheral(s) (PPP), for the  */
/*  available peripheral interrupt handler's name please refer to the startup */
/*  file (startup_stm32f10x_xx.s).                                            */
/******************************************************************************/
extern __IO uint8_t RxCounter;
extern __IO uint8_t TxCounter;
extern uint8_t RxBuffer[];
extern uint8_t TxBuffer[];
extern uint8_t NbrOfDataToRead;
extern uint8_t NbrOfDataToTransfer;

extern volatile uint16_t low_adc_ready;
extern volatile uint16_t low_adc_value;

void USART3_IRQHandler(void)
{
  if (USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)
  {
    RxBuffer[RxCounter++] = USART_ReceiveData(USART3);
    // if (RxCounter == NbrOfDataToRead)
    // {
    //   RxCounter = 0;
    //   // USART_ClearFlag(USART3, USART_IT_RXNE);
    //   USART_ITConfig(USART3, USART_IT_RXNE, DISABLE);
    // }
  }
  if (USART_GetITStatus(USART3, USART_IT_TXE) != RESET)
  {
    USART_SendData(USART3, TxBuffer[TxCounter++]);
    if (TxCounter == NbrOfDataToTransfer)
    {
      TxCounter = 0;
      // USART_ClearFlag(USART3, USART_IT_TXE);
      USART_ITConfig(USART3, USART_IT_TXE, DISABLE);
    }
  }
}

// TIM2中断服务函数
void TIM2_IRQHandler(void)
{
  if (TIM_GetITStatus(TIM2, TIM_IT_CC2) != RESET)
  {
    // 清除TIM2的CC2中断标志
    TIM_ClearITPendingBit(TIM2, TIM_IT_CC2);  
    

    // 启动ADC转换
    //ADC_SoftwareStartConvCmd(ADC1, ENABLE);
    // 注意：此处无需手动启动ADC转换，因为ADC已配置为外部触发模式！
        // 触发信号由TIM2自动发送给ADC。
  }
}

// ADC1中断服务函数
void ADC1_2_IRQHandler(void)
{
  if (ADC_GetITStatus(ADC1, ADC_IT_EOC) != RESET)
  {
    // 清除ADC转换完成中断标志
    ADC_ClearITPendingBit(ADC1, ADC_IT_EOC);

    // 读取ADC值
    low_adc_value = ADC_GetConversionValue(ADC1);

    // 设置ADC转换完成标志
    low_adc_ready = 1;
  }
}

/**
 * @brief  This function handles PPP interrupt request.
 * @param  None
 * @retval None
 */
/*void PPP_IRQHandler(void)
{
}*/

/**
 * @}
 */
